package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.AppointmentTalentsEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.TalentsEntity;
import io.renren.modules.sys.service.AppointmentService;
import io.renren.modules.sys.service.AppointmentTalentsService;
import io.renren.modules.sys.service.TalentsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-01-04 00:11:36
 */
@RestController
@RequestMapping("sys/appointmenttalents")
public class AppointmentTalentsController  extends AbstractController {
    @Autowired
    private AppointmentTalentsService appointmentTalentsService;

    @Autowired
    private TalentsService talentsService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
    	SysUserEntity sysUserEntity=getUser();
    	params.put("userId", sysUserEntity.getUserId());
    	Map<String, Object> params2=new HashMap<>();
		params2.put("date", new Date());
    	PageUtils pageUtils2=appointmentTalentsService.queryPage(params2);
		for (Object appointmentEntity : pageUtils2.getList()) {
			AppointmentTalentsEntity appointmentEntity2=(AppointmentTalentsEntity) appointmentEntity;
			appointmentTalentsService.deleteById(appointmentEntity2.getId());
		}
        PageUtils page = appointmentTalentsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        AppointmentTalentsEntity appointmentTalents = appointmentTalentsService.selectById(id);

        return R.ok().put("appointmentTalents", appointmentTalents);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AppointmentTalentsEntity appointmentTalents){
    	SysUserEntity sysUserEntity=getUser();
    	appointmentTalents.setAddDate(new Date());
    	appointmentTalents.setUserId(sysUserEntity.getUserId().intValue());
    	appointmentTalents.setUserName(sysUserEntity.getUsername());
    	TalentsEntity talents=talentsService.selectById(appointmentTalents.getTalentsId());
    	appointmentTalents.setTalentsId(talents.getId());
    	appointmentTalents.setTalentsName(talents.getName());
        appointmentTalentsService.insert(appointmentTalents);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AppointmentTalentsEntity appointmentTalents){
        ValidatorUtils.validateEntity(appointmentTalents);
        appointmentTalentsService.updateAllColumnById(appointmentTalents);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        appointmentTalentsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
